package com.bit.springblogdemo.controller;

import com.bit.springblogdemo.pojo.request.AddBlogRequest;
import com.bit.springblogdemo.pojo.request.UpdateBlogRequest;
import com.bit.springblogdemo.pojo.response.BlogInfoResponse;
import com.bit.springblogdemo.service.BlogService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import jakarta.validation.constraints.NotNull;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RequestMapping("/blog")
@RestController
@Slf4j
public class BlogController {
    @Resource(name = "blogServiceImpl")
    private BlogService blogService;

    /*获取博客列表*/
    @RequestMapping("/getList")
    public List<BlogInfoResponse> getList(){
        log.info("获取博客列表");
        List<BlogInfoResponse> blogInfos = blogService.getList();
        return blogInfos;
    }

    /*获取博客详情*/
    @RequestMapping("/getBlogDetail")  //这个主注解用于拦截，
                                          // 表示blogId为null压根不会执行下面代码
    public BlogInfoResponse getBlogDetail(@NotNull(message = "blogId不能为空") Integer blogId){
        log.info("获取博客详情，blogId:{}",blogId);
        return blogService.getBlogDetail(blogId);
    }

    /*添加博客*/
    @RequestMapping("/add")
    public Boolean addBlog(@RequestBody @Validated AddBlogRequest addBlogRequest){
        log.info("添加博客， userId: {},title: {}",addBlogRequest.getUserId(),addBlogRequest.getTitle());
        return blogService.addBlog(addBlogRequest);
    }

    /*编辑博客*/
    @RequestMapping("/update")
    public  Boolean update(@RequestBody @Validated UpdateBlogRequest updateBlogRequest){
        log.info("跟新博客, request {}",updateBlogRequest);
        return blogService.updateBlog(updateBlogRequest);
    }

    /*删除博客*/
    @RequestMapping("/delete")
    public Boolean deltet(@NotNull(message = "blogId 不能为null") Integer blogId){
        log.info("删除博客blogId:{}",blogId);
        return blogService.deleteBlog(blogId);
    }
}
